package com.cytech.cloudclinic.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.SelectImageAdapter;
import com.cytech.cloudclinic.bean.UploadImage;


import java.util.ArrayList;
import java.util.List;

/**
 * 选择图片按钮
 */
public class SelectImageView extends RecyclerView {
    private static final String TAG = "SelectImageView";
    private Context context;
    private List<UploadImage> imageList;
    private SelectImageAdapter mAdapter;
    private OnItemImageSelectListener mOnItemImageSelectListener;
    private int maxCount = 1;
    //标记选中的是哪一个
    private int seleteTag = 1;

    public SelectImageView(@NonNull Context context) {
        this(context,null);
    }

    public SelectImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SelectImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initParam(attrs);
        initView();
    }

    private void initView() {
        this.setLayoutManager(new GridLayoutManager(context, 3));
        imageList = new ArrayList<>();
        UploadImage uploadImage = new UploadImage();
        imageList.add(uploadImage);
        mAdapter = new SelectImageAdapter(imageList);
        setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                UploadImage selectImage = mAdapter.getItem(position);
                if(selectImage.isDefaultImage){
                    //如果是默认图片的话，则打开相册
                    if(mOnItemImageSelectListener !=null){
                        mOnItemImageSelectListener.onImageSelectSelect(seleteTag);
                    }
                }
            }
        });
        //删除图片
        mAdapter.addChildClickViewIds(R.id.iv_common_delete);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Log.d(TAG, "onItemChildClick: "+position);
                if(mOnItemImageSelectListener!=null){
                    if(mAdapter.getItemCount() == maxCount){
                        mOnItemImageSelectListener.onImageDelete(position,seleteTag);
                        mAdapter.remove(position);
                        if(!TextUtils.isEmpty(mAdapter.getItem(mAdapter.getItemCount()-1).url)){
                            mAdapter.addData(new UploadImage());
                        }
                    }else{
                        mOnItemImageSelectListener.onImageDelete(position,seleteTag);
                        mAdapter.remove(position);
                    }
                }
            }
        });
    }

    public void notifySelectData(UploadImage uploadImage){
        if(mAdapter.getData().size()>0){
            mAdapter.addData(mAdapter.getData().size()-1,uploadImage);
            //如果达到最大值了，则移除添加按钮，-1是减去默认的图片
            if(mAdapter.getItemCount()-1==maxCount){
                mAdapter.remove(mAdapter.getItemCount()-1);
            }
        }
    }

    public List<UploadImage> getAllData(){
        return  mAdapter.getData();
    }

    public interface OnItemImageSelectListener {
        void onImageSelectSelect(int seleteTag);

        void onImageDelete(int position,int seleteTag);
    }

    public void setOnItemSelectListener(OnItemImageSelectListener onItemImageSelectListener){
        this.mOnItemImageSelectListener = onItemImageSelectListener;
    }

    public void setOnItemSelectListener(OnItemImageSelectListener onItemImageSelectListener,int seleteTag){
        this.mOnItemImageSelectListener = onItemImageSelectListener;
        this.seleteTag = seleteTag;
    }

    public void initParam(@Nullable AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SelectImageView);
        maxCount = ta.getInteger(R.styleable.SelectImageView_select_image_max_count,1);
        //注意回收
        ta.recycle();
    }

}
